package offer.offer01;

/**
 *
 */
public class S11旋转数组的最小数字 {
    /**
      * 类似于二分查找, 通过比较中间值与两个边界的值, 判断中间值所处的地点
      */
    public int minArray(int[] numbers) {
        int inputLength = numbers.length;
        if(inputLength <= 2) {
            printArray(numbers);
            if(inputLength == 1) return numbers[0];
            else {
                return myMin(numbers[0], numbers[1]);
            }
        }
        int midLoc = inputLength/2;
        int a = numbers[0], b = numbers[midLoc], c = numbers[inputLength - 1];
        System.out.println("" + a + "  " + b + "  " + c);
        if(a>c) {
            if(b >= a) {
                return minArray(java.util.Arrays.copyOfRange(numbers, midLoc, inputLength));
            }else {
                return minArray(java.util.Arrays.copyOfRange(numbers, 0, midLoc + 1));
            }
        }else if(a == c){
            //a == c
            if(b > a) {
                return minArray(java.util.Arrays.copyOfRange(numbers, midLoc, inputLength));
            }else if(b < a) {
                return minArray(java.util.Arrays.copyOfRange(numbers, 0, midLoc + 1));
            }else {
                return myMin(minArray(java.util.Arrays.copyOfRange(numbers, midLoc, inputLength)), minArray(java.util.Arrays.copyOfRange(numbers, 0, midLoc + 1)));
            }
        }else {
            return numbers[0];
        }
    }
    public int myMin(int a, int b) {
        return a > b? b: a;
    }
    public void printArray(int[] a) {
        for(int i : a) {
            System.out.print(i + " ");
        }
    }
}
